package de.fhd.pms.model;

import java.awt.List;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
/**
 * Basic Model Class
 */
@Entity
@Table(name = "tour")
public class Tour {

	@Id
	@GeneratedValue
	// @Column(name = "tour_id")
	private Integer id;
	@ManyToOne(fetch = FetchType.EAGER)
	User user;
	@ManyToOne
	//@ForeignKey(name = "action")
	@NotNull
	Action action;
	// @Column(name = "user_id")
	// private Integer user_id;
	// @Column(name = "action_id")
	// private Integer action_id;
	@Column(name = "end_date")
	private Date end_date;
	@Column(name = "end_hours")
	private Integer end_hours;
	@Column(name = "end_minutes")
	private Integer end_minutes;
	@Column(name = "start_date")
	private Date start_date;
	@Column(name = "start_hours")
	private Integer start_hours;
	@Column(name = "start_minutes")
	private Integer start_minutes;
	@Column(name = "description")
	private String description;
	@Column(name = "max_member_count")
	private Integer max_member_count;
	@Column(name = "min_member_count")
	private Integer min_member_count;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Date getStart_date() {
		return start_date;
	}

	public void setStart_date(Date start_date) {
		this.start_date = start_date;
	}

	public Integer getStart_hours() {
		return start_hours;
	}

	public void setStart_hours(Integer start_hours) {
		this.start_hours = start_hours;
	}

	public Integer getStart_minutes() {
		return start_minutes;
	}

	public void setStart_minutes(Integer start_minutes) {
		this.start_minutes = start_minutes;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getMax_member_count() {
		return max_member_count;
	}

	public void setMax_member_count(Integer max_member_count) {
		this.max_member_count = max_member_count;
	}

	public Integer getMin_member_count() {
		return min_member_count;
	}

	public void setMin_member_count(Integer min_member_count) {
		this.min_member_count = min_member_count;
	}

	public Date getEnd_date() {
		return end_date;
	}

	public void setEnd_date(Date end_date) {
		this.end_date = end_date;
	}

	public Integer getEnd_hours() {
		return end_hours;
	}

	public void setEnd_hours(Integer end_hours) {
		this.end_hours = end_hours;
	}

	public Integer getEnd_minutes() {
		return end_minutes;
	}

	public void setEnd_minutes(Integer end_minutes) {
		this.end_minutes = end_minutes;
	}

	public Action getAction() {
		return action;
	}

	public void setAction(Action action) {
		this.action = action;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	@InitBinder
	public void binder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
			public void setAsText(String value) {
				try {
					setValue(new SimpleDateFormat("dd/MM/yyyy").parse(value));
				} catch (ParseException e) {
					setValue(null);
				}
			}

			public String getAsText() {
				return new SimpleDateFormat("dd/MM/yyyy")
						.format((Date) getValue());
			}

		});

	}
	
	@ManyToMany(/*cascade = CascadeType.ALL*/)
	@JoinTable(
			name = "user_tour", 
			joinColumns = { @JoinColumn(name = "tour_id") }, 
			inverseJoinColumns = { @JoinColumn(name = "user_id") })
	private Set<User> clients = new HashSet<User>();

	public Set<User> getClients() {
		return clients;
	}

	public void setClients(Set<User> clients) {
		this.clients = clients;
	}
	
	public boolean isClient(User u) {
		for (User client : clients) {
			if (client.getId().equals(u.getId())) {
				return true;
			}
		}
		return false;
	}
}
